<?

/*	ObjectList OBJECT
*	Methods:
*	output
*
*
*					*/


class ObjectList extends modTEXT {

	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
	}
	
	public function construct() {
		global $Object, $Page;
	
		if( !$id = (int) $_GET['id'] ) return false;
		
		if( !$Object ) {
			$Object = new OBJECT(( array ) $this->vars, $id );
			$Page->set_cache( "title", $Object->get( 'name' ));
		}

	}
	
	public function output() {
		
		$this->addclass( $this->action );
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		if( is_array( $result ))
			$result = "<ul>" . implode( "\n", $result ) . "</ul>";
			
		return $this->parse( $result ) . $this->render_js();
		
	}
	
	function filter_basic() {
		global $DB, $Children;
		
		if( $Children = $DB->filter_objects( $this->vars->type, $this->vars->condt )) {
		
			foreach( $Children as $obj ) {
				if( $this->vars->screentext ) {
					if( $obj->get( 'content' ) && $obj->get( 'content' ) != '...' )
						$result[] = $this->render_li( "<a href='{$obj->uri()}' class='title'>{$obj->get( 'name' )}</a>" );
				} elseif( $this->vars->screenchildren ) {
					if( $obj->ref_objects( $this->vars->reftype, $this->vars->refmaster ))
						$result[] = $this->render_li( "<a href='{$obj->uri()}' class='title'>{$obj->get( 'name' )}</a>" );
				} else {				
					$result[] = $this->render_li( "<a href='{$obj->uri()}' class='title'>{$obj->get( 'name' )}</a>" );
				}
			}
		}
		
		$result[] = "<li class='stable'></li>";
		
		return $result;
	}
	
	function filter_thumbs() {
		global $DB, $Object, $Thumb;
		
		////////// TEMP Object conflict solution ! ///////////
		if( $Object->type != $this->vars->type )
			$Object = new OBJECT( $this->vars->type, (int) $_GET[ 'id' ]);
		
		
		/// end
		
		$Thumb = new MEDIA( array( "sze"=> $this->vars->sze ));
		
		if( $Object && $result = $this->cycle_thumbs( $Object ))
			return $result;
	}
	
	function cycle_thumbs( $Object, $limit = 0 ) {
		global $DB, $Thumb;
	
		$list = array( $this->render_li( $Object->get( "name" ), "title", $limit? $Object->uri() : false ));

		// trace child groups
		if( $children = $Object->ref_objects( "group", 1, $limit? $limit : 240 ))
			foreach( $children as $Child )
				if( $Child->active())
					if( $sub = $this->cycle_thumbs( $Child, $this->vars->limit ))
							$result[] = "<ul class='group'>" . implode( "\n", $sub ) . "</ul>";
		
		// trace direct children 
		if( $children = $Object->ref_objects( $this->vars->objtype, !$this->vars->ischild, $limit? $limit : 240, $this->vars->table? $this->vars->table: "object" ))
			foreach( $children as $Child )
				if( $Child->active())
					$result[] = $this->render_thumb( $Child->get( "name" ), $Thumb->_img( $Child->media->get( "media.img" )), $Child->uri() );

		if( $result )
			$list[] = $this->render_li( implode( "\n", $result ));

		return $list;
	}
	
	function filter_showcase() {
		global $DB, $Object;
		
		if( !$condt = $this->vars->condt )
			 $condt = "active order by rank desc, rand() limit " . $this->vars->limit;
		

		$Children = ( $this->vars->global )?
			$DB->filter_objects( $this->vars->type, $condt ) :
			$Object->ref_objects( $this->vars->reftype, $this->vars->master );
			
		if( $Children )
		
			foreach( $Children as $Obj )
				
				if( $Obj->hasMedia()) {
				
					$img		= $Obj->media->uri( "media.img", $this->vars->sze );
					$parent		= ( $Parent = $Obj->ref_object( $this->vars->parenttype ))?
									"<a href='{$Parent->uri()}' class='parent'>{$Parent->get( 'name' )}</a>" : "";
					
					$result[]	= "<li style='background-image:url($img)'><span class='name'><a href='{$Obj->uri()}'>{$Obj->get( 'name' )}</a>$parent</span><span class='content'>{$Obj->get( 'content' )}</span><span class='period'>{$Obj->get( 'period' )}</span></li>";
				}

		
		return $result;
	}

	/**
	 * 	Tagcloud inspired object cloud	
	 */
	function filter_cloud() {
		global $DB; 
		
		
		$list = $DB->filter_objects( $this->vars->type, "active order by rank desc, rand() limit " . $this->vars->limit );
		
		if( shuffle( $list )) {
			foreach( $list as $Obj )
				
				$return[] = "<a href='{$Obj->uri()}' class='rank{$Obj->get( 'rank' )}'>{$Obj->get( 'name' )}</a>";
		
		
			return implode( " ", $return );
		}	
	}
	
	function filter_reference() {
		global $DB, $Object;
	
		if( $refs = $Object->ref_objects( $this->vars->reftype, $this->vars->master ))
			
			foreach( $refs as $Ref )
				$content[] = "<li><a href='{$Ref->uri()}'>{$Ref->get( 'name' )}</a></li>";
			
		
		return $content;
		
	}
	
	function filter_condt() {
		global $DB;
		
		
		if( $Children = $DB->filter_objects( $this->vars->type, $this->vars->condt )) {
		
			foreach( $Children as $obj ) {
			
				$string = ( $sze = $this->vars->imgsize )? "<img src='/public.img?sze=$sze&id={$obj->media->get( "media.img" )->ID}' alt=\"{$obj->get('name')}\" />" : "";
				foreach( $this->vars->lisort as $key )
					$string .= "<span class='$key'>{$obj->get( $key )}</span>";
				if( $this->vars->more ) $string .= "<a href='{$obj->uri()}' class='more'>{$this->vars->more}</a>";
				
				$result[] = $this->render_li( $string );
			}
		} else {
		
			$result[] = 'empty.';
		}
		
		$result[] = "<li class='stable'></li>";
		
		return $result;
		
	}

	function render_li( $content, $class = "", $uri = false ) {
		
		if( $class )	$class = " class='$class'";
		if( $uri )		$content = "<a href='$uri'>$content<span class='more'>" . translate( "meer..." ) . "</span></a>";
		
		return "<li$class>$content</li>";
	}
	
	function render_thumb( $name, $img, $uri ) {
		
		return "<span class='thumb'><a href='$uri'>$img$name</a></span>";
	}
	
} 



?>